package com.cn.service.server.ComponentMechanical.Impl;

import com.cn.bean.pojo.ComponentMechanical.RepairApplicationMain;
import com.cn.bean.pojo.ComponentMechanical.RepairApplicationSub;
import com.cn.bean.pojo.ComponentMechanical.RepairOutsourcingMainTable;
import com.cn.bean.pojo.ComponentMechanical.RepairOutsourcingSubTable;
import com.cn.bean.pojo.TechnologicalStandard.GrindingProcessStandardMain;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db1.bc.Mate;
import com.cn.mapper.dao.db1.bc.PurposesDao;
import com.cn.mapper.dao.db5.ComponentMechanical.OutsourcingResultDao;
import com.cn.mapper.dao.db5.ComponentMechanical.RepairApplicationDao;
import com.cn.mapper.dao.db5.TechnologicalStandard.GrindingProcessStandardMainDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.ComponentMechanical.RepairApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RepairApplicationServiceImpl implements RepairApplicationService {

    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    private final RepairApplicationDao repairApplicationDao;

    @Autowired(required = false)
    public RepairApplicationServiceImpl(RepairApplicationDao repairApplicationDao, Sys_Sequence_Info_TDAO sysSequenceInfoTdao) {

        this.sysSequenceInfoTdao = sysSequenceInfoTdao;

        this.repairApplicationDao = repairApplicationDao;

    }


    @Override
    public List<RepairApplicationSub> selectRepairApplicationMain(int page, int limit, String equipmentName, String uniqueCode) {
        return repairApplicationDao.selectRepairApplicationMain(page, limit, equipmentName, uniqueCode);
    }

    @Override
    public List<RepairApplicationMain> selectRepairApplicationMainAll(int page, int limit, String code) {
        return repairApplicationDao.selectRepairApplicationMainAll(page, limit, code);
    }

    @Override
    public List<RepairApplicationSub> selectRepairSubTable(RepairApplicationSub repairApplicationSub) {
        return repairApplicationDao.selectRepairSubTable(repairApplicationSub);
    }

    @Override
    public List<RepairOutsourcingSubTable> selectRepairOutsourcingSub(int id) {
        return repairApplicationDao.selectRepairOutsourcingSub(id);
    }

    @Override
    public int insertRepairOutsourcingMainTable(RepairOutsourcingMainTable repairOutsourcingMainTable) {
        Boolean a = repairApplicationDao.insertRepairOutsourcingMainTable(repairOutsourcingMainTable);
        if (a) {
            return 1;
        }
        return 0;
    }

    @Override
    public int insertRepairOutsourcingSubTable(RepairOutsourcingSubTable repairOutsourcingSubTable) {
        Boolean b = repairApplicationDao.insertRepairOutsourcingSubTable(repairOutsourcingSubTable);
        if (b) {
            return 1;
        }
        return 0;
    }

    @Override
    public int openRepairOutsourcing(String uniqueCode) {
        Boolean c = repairApplicationDao.openRepairOutsourcing(uniqueCode);
        if (c) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateReturn(RepairApplicationMain repairApplicationMain) {
        Boolean d = repairApplicationDao.updateReturn(repairApplicationMain);
        if (d) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateApplicationAudit(int id) {
        Boolean e=repairApplicationDao.updateApplicationAudit(id);
        if (e){
            return 1;
        }
        return 0;
    }

    @Override
    public int updateRepairOutsourcingAudit(int id) {
        Boolean f=repairApplicationDao.updateRepairOutsourcingAudit(id);
        if (f){
            return 1;
        }
        return 0;
    }

    @Override
    public int delRepairOutsourcingSubTable(int id) {
        Boolean g=repairApplicationDao.delRepairOutsourcingSubTable(id);
        if (g){
            return 1;
        }
        return 0;
    }

    @Override
    public int updateReturnSub(RepairApplicationSub repairApplicationSub) {
        Boolean h=repairApplicationDao.updateReturnSub(repairApplicationSub);
        if (h){
            return 1;
        }
        return 0;
    }

    @Override
    public int completeRepairOutsourcing(String uniqueCode) {
        Boolean j=repairApplicationDao.completeRepairOutsourcing(uniqueCode);
        if (j){
            return 1;
        }
        return 0;
    }

    @Override
    public Integer getRepairOutsourcingSubAutoid() {
        return repairApplicationDao.getRepairOutsourcingSubAutoid();
    }

    @Override
    public List<RepairApplicationMain> selectRepairOutsourcingAll(int page, int limit, String code) {
        return repairApplicationDao.selectRepairOutsourcingAll(page, limit, code);
    }

    @Override
    public Result addRepairApplicationMain(RepairApplicationMain repairApplicationMain, List<RepairApplicationSub> repairApplicationSubList) {
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("RepairApplicationMainTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "RepairApplicationMainTable");
        repairApplicationMain.setId(ID);
        repairApplicationMain.setCode(code);
        int i = repairApplicationDao.addRepairApplicationMain(repairApplicationMain);
        for (RepairApplicationSub rep : repairApplicationSubList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("RepairApplicationSubTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            repairApplicationDao.updateComponentSingleRegistrationAuto(4, 4, rep.getUniqueCode(), 3);
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "RepairApplicationSubTable");
            rep.setId(ID1);
            rep.setMid(ID);
            int i1 = repairApplicationDao.addRepairApplicationSub(rep);

        }

        return Result.success("200", "添加成功");
    }

    @Override
    public Result deleteRepairApplicationMain(List<Integer> idList) {
        for (Integer i : idList) {
            List<RepairApplicationMain> repairApplicationMains = repairApplicationDao.selectEchoRepairApplicationMainAll(i);
            RepairApplicationMain repairApplicationMain = repairApplicationMains.get(0);
            if (repairApplicationMain.getState() == 1) {
                return Result.failure("500", "已审核的数据，不能进行删除操作");
            }

        }
        for (Integer i : idList) {
            repairApplicationDao.deleteRepairApplicationMain(i);
        }
        return Result.success("200", "删除成功");
    }

    @Override
    public List<RepairApplicationMain> selectEchoRepairApplicationMainAll(int id) {
        List<RepairApplicationMain> repairApplicationMains = repairApplicationDao.selectEchoRepairApplicationMainAll(id);
        List<RepairApplicationSub> repairApplicationSubs = repairApplicationDao.selectEchoRepairSubTable(id);
        RepairApplicationMain repairApplicationMain = repairApplicationMains.get(0);
        repairApplicationMain.setRepairApplicationSubList(repairApplicationSubs);
        return repairApplicationDao.selectEchoRepairApplicationMainAll(id);
    }

    @Override
    public Result updateRepairApplicationMain(RepairApplicationMain repairApplicationMain, List<RepairApplicationSub> repairApplicationSubList) {
        List<RepairApplicationMain> repairApplicationMains = repairApplicationDao.selectEchoRepairApplicationMainAll(repairApplicationMain.getId());
        RepairApplicationMain repairApplicationMain1 = repairApplicationMains.get(0);
        if (repairApplicationMain1.getState() == 1) {
            return Result.failure("500", "已审核的数据，不能进行修改操作");
        }
        if (repairApplicationMain1.getState() == -1) {
            return Result.failure("500", "已删除的数据，不能进行修改操作");
        }
        repairApplicationDao.updateRepairApplicationMain(repairApplicationMain);
        repairApplicationDao.deleteRepairApplicationSub(repairApplicationMain.getId());

        //获取修改前的子表；
        List<RepairApplicationSub> repairApplicationSubs = repairApplicationDao.selectEchoRepairSubTable(repairApplicationMain.getId());
        for (RepairApplicationSub res : repairApplicationSubs) {
            repairApplicationDao.updateComponentSingleRegistrationAuto(4, 4, res.getUniqueCode(), 1);
        }

        for (RepairApplicationSub rep : repairApplicationSubList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("RepairApplicationSubTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            repairApplicationDao.updateComponentSingleRegistrationAuto(4, 4, rep.getUniqueCode(), 3);

            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "RepairApplicationSubTable");
            rep.setId(ID1);
            rep.setMid(repairApplicationMain.getId());
            int i1 = repairApplicationDao.addRepairApplicationSub(rep);

        }
        return Result.success("200", "修改成功");
    }

    @Override
    public Result updateRepairApplicationMainAuto(List<Integer> idList, String reviewer, String reviewerID, String reviewTime) {
        for (Integer i : idList) {
            List<RepairApplicationMain> repairApplicationMains = repairApplicationDao.selectEchoRepairApplicationMainAll(i);
            RepairApplicationMain repairApplicationMain = repairApplicationMains.get(0);
            if (repairApplicationMain.getState() == -1) {
                return Result.failure("500", "已删除的数据，不能进行审核操作");
            }
            if (repairApplicationMain.getState() == 1) {
                return Result.failure("500", "已审核的数据，不能重复审核操作");
            }
            if (repairApplicationMain.getRepairMode() == 0&&repairApplicationMain.getTotalAmount()==null) {
                return Result.failure("500", "请修改总金额");
            }
        }

        for (Integer i : idList) {
            List<RepairApplicationMain> repairApplicationMains = repairApplicationDao.selectEchoRepairApplicationMainAll(i);
            //对应主表
            RepairApplicationMain repairApplicationMain = repairApplicationMains.get(0);
            repairApplicationDao.updateRepairApplicationMainAuto(repairApplicationMain.getId(), reviewer, reviewerID, reviewTime);
            //如果为自磨。单件状态（1）完成：物料状态为0（待登记）；流转状态为1（待耗用）
            //                （2）报废：物料状态为5（报废）；流转状态为4（异常）
            //委外：物料状态为4，流转状态为4
            if (repairApplicationMain.getRepairMode() == 1) {
                return Result.success("200", "审核成功");
            }
            List<RepairApplicationSub> repairApplicationSubs = repairApplicationDao.selectEchoRepairSubTable(repairApplicationMain.getId());
            for (RepairApplicationSub rep : repairApplicationSubs) {

                if (rep.getSingleStatus() == 0) {
                    //完成
                    repairApplicationDao.updateComponentSingleRegistrationAuto(0, 1, rep.getUniqueCode(), 0);
                } else {
                    //报废
                    repairApplicationDao.updateComponentSingleRegistrationAuto(5, 4, rep.getUniqueCode(), 0);
                }
            }

        }
        return Result.success("200", "审核成功");
    }

}
